package zozopo.warehouse;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.flow.operations.RecordlistOS;
import zozopo.flow.operations.RecordlistTB;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.AftersaleserviceOS;
import zozopo.warehouse.operations.AftersaleserviceTB;
import zozopo.warehouse.operations.BackreceiptlistOS;
import zozopo.warehouse.operations.BackreceiptlistTB;
import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.PurchaseTB;
import zozopo.warehouse.operations.ReceiptlistTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ManageBackreceiptlistAction extends ActionSupport implements Action,SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String limitedCode ="WAREHOUSE.BACKRECEIPTLIST";
	
	//传入参数
	private String searchStr = "";
	//分页参数
	private int beginPageNum = 0;
	private int pageSize = 20;
	private int rowNum = 0; 
	
	//传出参数
	private List<BackreceiptlistTB> backreceiptlistList;
	private Map<Integer,List<PurchaseTB>> purchaseListMap;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	public String execute(){
		BackreceiptlistOS backreceiptlistOS = new BackreceiptlistOS();
		PurchaseOS purchaseOS = new PurchaseOS(backreceiptlistOS.getConn());

		if(searchStr.equals("")){
			backreceiptlistList = backreceiptlistOS.selectAll(beginPageNum, pageSize);
			rowNum = backreceiptlistOS.getRowNum();
		}else{
			backreceiptlistList = backreceiptlistOS.selectBySearchStr(searchStr, beginPageNum, pageSize);
			rowNum = backreceiptlistOS.getRowNumForSearchStr(searchStr);
		}
		
		String inStr = "(";
		boolean isFirst = true;
		
		if(backreceiptlistList == null){
			return null;
		}
		
		for(BackreceiptlistTB backreceiptlist:backreceiptlistList){
			if(isFirst){
				inStr += backreceiptlist.getId_backreceiptlist();
				isFirst = false;
			}else{
				inStr += "," + backreceiptlist.getId_backreceiptlist();
			}
		}
		inStr +=")";
		
		
		purchaseListMap = purchaseOS.getListMapByBackreceiptlistid(inStr);

		backreceiptlistOS.releaseConnection();
		 
		return SUCCESS;
	}
	
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有查看退货权限.");
			return;
		}
	}
	
	public String getSearchStr() {
		return searchStr;
	}
	public void setSearchStr(String searchStr) throws UnsupportedEncodingException {
		this.searchStr = java.net.URLDecoder.decode(searchStr, "UTF-8");
		this.searchStr = this.searchStr.trim();
	}
	public int getBeginPageNum() {
		return beginPageNum;
	}
	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRowNum() {
		return rowNum;
	}
	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	public List<BackreceiptlistTB> getBackreceiptlistList() {
		return backreceiptlistList;
	}

	public void setBackreceiptlistList(List<BackreceiptlistTB> backreceiptlistList) {
		this.backreceiptlistList = backreceiptlistList;
	}

	public Map<Integer, List<PurchaseTB>> getPurchaseListMap() {
		return purchaseListMap;
	}

	public void setPurchaseListMap(Map<Integer, List<PurchaseTB>> purchaseListMap) {
		this.purchaseListMap = purchaseListMap;
	}

	public static String getLimitedcode() {
		return limitedCode;
	}

	

}
